import Vue from 'vue'
import {ACCESS_TOKEN} from '@/store/mutation-types'
import {downFile} from "@api/manage";

export const TableMixin = {
  data() {
    return {
      alert: {
        clear: true
      },
      selectedRowKeys: [],
      selectedRows: [],
      queryParam: {},
      // file-upload
      headers: {
        Authorization: 'Bearer ' + Vue.ls.get(ACCESS_TOKEN)
      }
    }
  },
  methods: {
    handlerQuery(e) {
      this.queryParam = {...e}
      this.queryPage()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleSubmit(e) {
      e.preventDefault()
      this.queryPage()
    },
    queryPage(bool = false) {
      this.$refs.table.refresh(bool)
    },
    showData(record) {
      this.$refs.modalForm.init(record.id, 'show')
    },
    // 点击新增
    addData(record) {
      this.$refs.modalForm.init('', 'add')
    },
    // 点击导入
    showModal() {
      this.$refs.BImportFile.show()
    },
    editData(record) {
      this.$refs.modalForm.init(record.id, 'edit')
    },
    exportData(url, name) {
      downFile(url, this.queryParam)
        .then(response => {
          let data = response
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          name = name + ".xlsx"
          let blob = new Blob([data])
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(blob, name)
          } else {
            let url = window.URL.createObjectURL(blob)
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', name)
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) //下载完成移除元素
            window.URL.revokeObjectURL(url) //释放掉blob对象
          }
        })
    }
  }
}
